<?php

require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_COST_FLIER.php' );
require_once( '../system/mdao/Class_M_COST_OTHER.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/mdao/Class_M_USER.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST_QR, $login['staff_auth']);
$smarty->assign('now_menu', $aokimenu[AOKI_AUTH_COST_QR]["li"]);

$dosearch = FALSE ;
$dao = new Class_M_COST_FLIER ;
//ゾーンとエリアの計算開始
//require_once( '../system/cell_ajareks_sort.inc.php' );
//ゾーンとエリアの計算終了

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	 $forms =  $_SESSION['MSEARCH']['COST_FLIER'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['COST_FLIER'] = $forms;
}

$login_auth = $login['staff_auth'];
$smarty->assign('login_auth',$login_auth);

if ($login_auth == 'G-2' ) {
	$area_cd   = $login['autoid'];
	$forms["sort"] = '2';
	$areauser = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else if ($login_auth == 'G-3' ) {
	$zone_cd   = $login['autoid'];
	$forms["sort"] = '3';
	$cellsort = array();
	$zoneuser = array( $aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else {
	$zoneuser  = array();
	$zoneuser  = Class_M_OTHER::get_ajaeks_array(PLEASE_SELECT_ARR, 3);
	$areauser = array();
	$areauser =  Class_M_OTHER::get_ajaeks_array(PLEASE_SELECT_ARR, 2);
	$zone_cd = array();
	if ( !isset( $forms['sort'])) {$forms["sort"]='3';}

}

$wherearr =  array();
$shop_name = NULL;
if ( isset( $forms['shop_name'] ) &&  ( !empty( $forms['shop_name'] ) ) ){
	//$wherearr[] = sprintf(" shop_name like  '%s' ",  "%".$forms['shop_name']."%" );
	$shop_name = $forms['shop_name'];
}


$martrix_cd = "";
if ( isset( $forms['week_cd'] ) &&  ( !empty( $forms['week_cd'] ) ) ){
	//$wherearr[] = sprintf("c.matrix_cd =  '%s' ",  $forms['week_cd'] );
	$martrix_cd   =  $forms['week_cd'];
}

if  (($forms["sort"] == '2') &&  $forms["area_aja"]) {

	$wherearr[] = sprintf(" a.area_aja =  %s ",  $dao->esc( $forms['area_aja'] ) );
}

if  (($forms["sort"] == '3') &&  $forms["zone_aja"]) {

	$wherearr[] = sprintf(" a.zone_aja =  %s ",  $dao->esc( $forms['zone_aja'] ) );
}

//---CSVダウンロード　処理開始

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){

	$_GET["csv"] = "";
	$tmpfilename = "cost_flier".date("Ymd_His");
	header("Content-Type: application/octet-stream");
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$colums = array();
	$col_heads = array(
		'matrix_cd'        => array( 'マトリックスID',  1),
		'year4'            => array( '年度',            1),
		'week_cd'          => array( '週',              1),
		'flier_date'       => array( '折込日',          1),
		'week_date_begin'  => array( '開始日',          1),
		'week_date_end'    => array( '終了日',          1),
		'shop_cd'          => array( '店舗コード',          1),
		'shop_name'        => array( '店名',          1),
		'area_aja'         => array( 'エリアAJA名',     1),
		'zone_aja'         => array( 'ゾーンAJA名',     1),
		'now_cost'         => array( '経費 本年(見込)', 1),
		'plane_cost'       => array( '経費 計画',       1),
		'last_cost'        => array( '経費 前年',       1),
		'plane_rate'       => array( '経費 計画費',     1),
		'plane_inc'        => array( '経費 計画差異',   1),
		'last_rate'        => array( '経費 前年比',     1),
		'last_inc'         => array( '経費 前年差異',   1),
	);

	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	$data = $dao->search( $wherearr, $page, $forms['week_cd'], $matr["pre_matrix_cd"] );
	
	//表示ために整形開始
	$matr = Class_M_OTHER::get_by_matrix_cd( $forms['week_cd']);
	
	$matrix_cd   =  $forms['week_cd'];
	
	$year4       =  $matr["year4"];
	$week_cd     =  "第".$matr["week_cd"]."週";
	if (!empty($matr["week_seq"])) {
		$week_cd     =  $week_cd.$matr["week_seq"]."回";
	}
	
	$flier_date       =  Class_PWD::int8_to_date( $matr["flier_date"] );
	$week_date_begin  =  Class_PWD::int8_to_date( $matr["week_date_begin"] );
	$week_date_end    =  Class_PWD::int8_to_date( $matr["week_date_end"] );
	
	foreach ($data as $k => $v ) {
		Class_PWD::set_csv_rate_inc($v);
		$v["matrix_cd"]       = $matrix_cd;
		$v["year4"]           = $year4;
		$v["week_cd"]         = $week_cd;
		$v["flier_date"]      = $flier_date;
		$v["week_date_begin"] = $week_date_begin;
		$v["week_date_end"]   = $week_date_end;
		
		$data[$k] = $v;
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
	if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
		$dosearch = TRUE;
	}

	if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
		$dosearch = TRUE;
		$page['current'] = $forms['page'];
	}else{
		$page['current'] = 1;
	}


	$page['item'] = PAGE_MAX;
	if ($dosearch) {
		$matr = Class_M_OTHER::get_by_matrix_cd( $forms['week_cd']);
	
		//$page内容は関数内部更新されます。
		$data = $dao->search( $wherearr, $page, $forms['week_cd'], $matr["pre_matrix_cd"] );
		
		//表示ために整形開始

		foreach ($data as $k => $v ) {
			Class_PWD::set_rate_inc($v);
			$data[$k] = $v;
		}

	}

//表示ために整形完了
if (empty($forms["year4"])) {
	//$forms["year4"] = date('Y');
	$forms["year4"] = date('Y');
}

$smarty->assign('year4',  Class_PWD::get_years());
$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );
$smarty->assign('week', $week );
//test
if (empty($forms["week_cd"])) {
	$forms["week_cd"] = "";
}

//週名を設定する
foreach ($week as $k => $v) {
	if ($k == $martrix_cd){
		$smarty->assign('weekname', $v );
		break;
	}
}

//ゾーンとエリア名称を設定する
if (($login_auth == 'G-2' ) || ($login_auth == 'G-3' ))  {
	foreach ($aoki_area_zone as $k => $v) {
		if ($k == $forms["sort"] ) {
			$aoki_area_zones[$k] = $v;
		} 
	}
}else {
	$aoki_area_zones[""] = "すべて";
	foreach ($aoki_area_zone as $k => $v) {
		$aoki_area_zones[$k] = $v;
	}
}

$smarty->assign('sorts', $aoki_area_zones );

//出力形式
/*
$outputlayoutstmp = array();
foreach ($outputlayout as $k => $v) {
	$outputlayoutstmp[$k] = $v;
}
$forms["outputlayout"]='0';
$smarty->assign('outputlayouts', $outputlayoutstmp );
*/


if( isset($zoneuser) )    $smarty->assign('zoneuser', $zoneuser);
if( isset($areauser) )    $smarty->assign('areauser', $areauser);

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
//if( isset($disp) )  $smarty->assign('disp', $disp);
if( isset($page) )  $smarty->assign('page', $page);

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mcost_flier_index.html');
?>